import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:qunaimedical/components/primary_button.dart';
import 'package:qunaimedical/network/http.dart';
import 'package:qunaimedical/pages/sms_login.dart';
import 'package:qunaimedical/utils/common_util.dart';
import 'package:qunaimedical/utils/dialog_utils.dart';
import 'package:qunaimedical/utils/toast_util.dart';
import 'package:qunaimedical/values/urls.dart';

/// 绑定手机号界面
class BindPhonePage extends StatefulWidget {
  final String wxInfo;

  const BindPhonePage(this.wxInfo, {Key key}) : super(key: key);

  @override
  _BindPhonePageState createState() => _BindPhonePageState();
}

class _BindPhonePageState extends State<BindPhonePage> {
  TextEditingController _controller = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('绑定手机号'),
        elevation: 0,
      ),
      body: ListView(
        padding: EdgeInsets.all(20),
        children: [
          Container(
            margin: EdgeInsets.only(top: 20),
            child: Text(
              '首次使用请绑定手机号',
              style: TextStyle(
                  color: Colors.black87,
                  fontSize: 20,
                  fontWeight: FontWeight.bold),
            ),
          ),
          SizedBox(height: 50),
          TextField(
            keyboardType: TextInputType.phone,
            inputFormatters: [
              FilteringTextInputFormatter.digitsOnly,
              LengthLimitingTextInputFormatter(11),
            ],
            controller: _controller,
            decoration: InputDecoration(
              hintText: '请输入手机号',
              enabledBorder: UnderlineInputBorder(
                borderSide: BorderSide(color: Colors.grey[300]),
              ),
              focusedBorder: UnderlineInputBorder(
                borderSide: BorderSide(color: Colors.grey[300]),
              ),
            ),
          ),
          PrimaryButton(
            '获取短信验证码',
            onPressed: _getCode,
            margin: EdgeInsets.only(top: 40),
          ),
        ],
      ),
    );
  }

  void _getCode() async {
    CommonUtils.hideKeyboard(context);
    if (_controller.text.trim().length != 11) {
      ToastUtil.show('请输入有效的手机号。', context);
      return;
    }
    DialogUtils.showWaitDialog(context);
    var wxInfo = json.decode(widget.wxInfo);
    var data = await XHttp.postFormData(Urls.GET_VERIFICATION_CODE, {
      "phoneNumber": _controller.text.trim(),
      "wxId": wxInfo['unionid'],
      "avatar": wxInfo['headimgurl'],
      "fullname": wxInfo['nickname'],
    });
    Navigator.pop(context);
    if (data != null) {
      Map<String, dynamic> response = data as Map<String, dynamic>;
      if (response['code'] == 1) {
        Navigator.pushNamed(context, SMSLoginPage.routeName,
            arguments: _controller.text.trim());
      } else {
        ToastUtil.show(response['msg'], context);
      }
    }
  }
}
